﻿<Window x:Class="Chem4Word.UI.WebServices.PubChemSearch" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Chem4Word.UI.UIControls" xmlns:converters="clr-namespace:Chem4Word.UI.Converters" ShowInTaskbar="False" SizeToContent="WidthAndHeight" MinWidth="300" Topmost="True" Name="pubChemSearch" Activated="PubChemSearchActivated" Loaded="PubChemSearch_OnLoaded" Title="PubChem lookup service">
	<Window.Resources>
		<converters:NullToBooleanConverter x:Key="myNullToBooleanConverter" />
		<converters:NullOrEmptyStringToBooleanConverter x:Key="myNullOrEmptyStringToBooleanConverter" />
		<converters:ObservableCollectionToBooleanConverter x:Key="myObservableCollectionToBooleanConverter" />
		<converters:BackgroundConverter x:Key="myBackgroundConverter" />
		<Style TargetType="{x:Type Button}" x:Key="BasicButtonStyle">
			<Setter Property="Height" Value="25" />
			<Setter Property="Width" Value="70" />
			<Setter Property="Margin" Value="6,0,6,0" />
		</Style>

		<Style TargetType="{x:Type Button}" x:Key="myImportButtonStyle" BasedOn="{StaticResource BasicButtonStyle}">
			<Setter Property="IsEnabled" Value="{Binding ElementName=pubChemSearch, Path=SelectedItem, Converter={StaticResource myNullToBooleanConverter}}" />
		</Style>

		<Style TargetType="{x:Type Label}" x:Key="myLabelStyle">
			<Setter Property="Margin" Value="6" />
			<Setter Property="Content" Value="{Binding ElementName=pubChemSearch, Path=ErrorMessage}" />
			<Setter Property="Visibility" Value="Collapsed" />
			<Style.Triggers>
				<DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=ErrorMessage, Converter={StaticResource myNullOrEmptyStringToBooleanConverter}}" Value="False">
					<Setter Property="Visibility" Value="Visible" />
				</DataTrigger>
			</Style.Triggers>
		</Style>
        
        <Style TargetType="{x:Type Label}" x:Key="mySearchResultsLabel">
            <Setter Property="Margin" Value="6" />
            <Setter Property="Content" Value="{Binding ElementName=pubChemSearch, Path=SearchResultsAvailable}" />
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=SearchResultsAvailable, Converter={StaticResource myNullOrEmptyStringToBooleanConverter}}" Value="False">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type Button}" x:Key="myPrevButton">
            <Setter Property="Margin" Value="6" />
            <Setter Property="Width" Value="70"/>
            <Setter Property="Visibility" Value="Collapsed" />
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=SearchResultsAvailable, Converter={StaticResource myNullOrEmptyStringToBooleanConverter}}" Value="False">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=PrevResults}" Value="True">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type Button}" x:Key="myNextButton">
            <Setter Property="Margin" Value="6" />
            <Setter Property="Width" Value="70"/>
            <Setter Property="Visibility" Value="Collapsed" />
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=SearchResultsAvailable, Converter={StaticResource myNullOrEmptyStringToBooleanConverter}}" Value="False">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=MoreResults}" Value="True">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>



        <Style x:Key="mMyListViewStyle" TargetType="{x:Type ListView}">
			<Setter Property="Margin" Value="6" />
			<Setter Property="Visibility" Value="Collapsed" />
			<Style.Triggers>
				<DataTrigger Binding="{Binding ElementName=pubChemSearch, Path=Results, Converter={StaticResource myObservableCollectionToBooleanConverter}}" Value="True">
					<Setter Property="Visibility" Value="Visible" />
				</DataTrigger>
			</Style.Triggers>
		</Style>

		<Style x:Key="myListViewItemStyle" TargetType="{x:Type ListViewItem}">
			<Setter Property="Background">
				<Setter.Value>
					<Binding RelativeSource="{RelativeSource Self}" Converter="{StaticResource myBackgroundConverter}" />
				</Setter.Value>
			</Setter>
		</Style>

	</Window.Resources>
	<StackPanel Margin="12" Name="stackPanel1" VerticalAlignment="Top" Orientation="Vertical">
		<local:SearchTextBox x:Name="searchBox" VerticalAlignment="Top" Margin="6" SearchMode="Delayed" Search="SearchBoxSearch" LabelText="Search Term" />
        <StackPanel Margin="2" Name="resultsInfoPanel" VerticalAlignment="Top" Orientation="Horizontal">
            <Label Style="{DynamicResource myLabelStyle}"></Label>
            <Label Style="{DynamicResource mySearchResultsLabel}"></Label>
            <Button Style="{DynamicResource myPrevButton}" Name="prevButton" Click="PrevButton_Click" Content="&lt; Prev"></Button>
            <Button Style="{DynamicResource myNextButton}" Name="nextButton" Click="nextButton_Click" Content="Next &gt;"></Button>
        </StackPanel>
        <ListView MaxHeight="400" x:Name="resultsView" ItemContainerStyle="{StaticResource myListViewItemStyle}" Style="{DynamicResource mMyListViewStyle}" ItemsSource="{Binding ElementName=pubChemSearch, Path=Results}" SelectionChanged="ResultsViewSelectionChanged" MouseDoubleClick="ResultsViewMouseDoubleClick" KeyUp="ResultsViewKeyUp">
			<ListView.View>
				<GridView>
					<GridViewColumn>
						<GridViewColumn.CellTemplate>
							<DataTemplate>
								<TextBlock Background="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=Background}" TextWrapping="WrapWithOverflow" Text="{Binding  Path=Name}" MaxWidth="400" VerticalAlignment="Center" />
							</DataTemplate>
						</GridViewColumn.CellTemplate>
						<GridViewColumnHeader Content="Name" Tag="Name" Click="SortClick" />
					</GridViewColumn>
					<GridViewColumn>
						<GridViewColumn.CellTemplate>
							<DataTemplate>
								<TextBlock Background="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=Background}" TextWrapping="WrapWithOverflow" Text="{Binding  Path=Formula}" MaxWidth="200" />
							</DataTemplate>
						</GridViewColumn.CellTemplate>
						<GridViewColumnHeader Content="Formula" Tag="Formula" Click="SortClick" />
					</GridViewColumn>

				</GridView>
			</ListView.View>
		</ListView>

		<StackPanel Name="buttonPanel" VerticalAlignment="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
			<Button Style="{StaticResource myImportButtonStyle}" Name="importButton" Click="ImportButtonClick">Import</Button>
			<Button Style="{StaticResource BasicButtonStyle}" Name="cancelButton" Click="CancelButtonClick">Cancel</Button>
		</StackPanel>
	</StackPanel>
</Window>